import { message } from 'antd';
import { DES } from '@/utils/encrypt';

// 查找指定的元素在数组中的位置
export function arrayIndexOf(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == val) {
            return i;
        }
    }
    return -1;
};
// 通过索引删除数组元素
export function removeArray(arr, val) {
    var index = arrayIndexOf(arr, val);
    if (index > -1) {
        arr.splice(index, 1);
    }
    return arr
};

export function arrayUnique(arr) {
    let hash = [];
    for (let i = 0; i < arr.length; i++) {
        if (hash.indexOf(arr[i]) === -1) {
            hash.push(arr[i]);
        }
    }
    return hash;
}

export function autoFaceUrl(url) {
    const auth = window.sessionStorage.getItem('auth');
    const u = new URL(url);
    u.searchParams.set('auth', auth);
    return u.toString();

}

export function autoVideoUrl(url) {
    let params = {
        account: window.localStorage.getItem('username'),
        password: window.localStorage.getItem('passwd'),
        auth: window.sessionStorage.getItem('auth'),
    }
    if (params.password) {
        params.password = DES.decrypt(params.password, '7e96c6045551061d3e1ee76c802d2b45');
    }
    const u = new URL(url);
    for (const key of u.searchParams.keys()) {
        params.hasOwnProperty(key) && u.searchParams.set(key, params[key]);
    }
    return u.toString();
}

export function jumpTo(url, tab) {
    if (!url) {
        message.warn('无效的地址');
        return;
    }
    let regExp = new RegExp('^http(s)?://');
    if (regExp.test(url)) {
        window.open(url, tab ? tab : '_blank');
    } else {
        window.open('http://' + window.location.hostname + url, tab ? tab : '_blank');
    }
}

// 判断浏览器是否支持全屏
export function isFullScreenEnabled() {
    return document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled;
}

// 判断是否有已全屏的元素
export function hasFullScreenElement() {
    return document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement
}
// 将目标元素设置为全屏展示
export function setFullScreen(target) {
    if (isFullScreenEnabled()) {
        if (target.requestFullscreen) {
            target.requestFullscreen();
        }
        if (target.webkitRequestFullscreen) {
            target.webkitRequestFullscreen();
        }
        if (target.mozRequestFullScreen) {
            target.mozRequestFullScreen();
        }
        if (target.msRequestFullscreen) {
            target.msRequestFullscreen();
        }
    }
}
// 文档退出全屏
export function exitFullScreen() {
    if (hasFullScreenElement()) {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        }
        if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
        }
        if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        }
        if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    }
}